
package servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import model.Item;
import dao.ItemDBAO;

/**
 * This is a simple example of an HTTP Servlet.  It responds to the GET
 * method of the HTTP protocol.
 */
public class ItemDetailsServlet extends HttpServlet {
	
    private ItemDBAO itemDB;

    public void init() throws ServletException {
    	
    	itemDB = (ItemDBAO) getServletContext().getAttribute("itemDB");

        if (itemDB == null) {
            throw new UnavailableException("Couldn't get database.");
        }
    }

    public void destroy() {
    	itemDB = null;
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        
        //Get the identifier of the book to display
        String itemNumber = request.getParameter("itemNumber");
        
        // set headers and buffer size before accessing the Writer
        response.setContentType("text/html");
        response.setBufferSize(8192);

        PrintWriter out = response.getWriter();

        // then write the response
        out.println("<html>" + "<head><title>details of " + itemNumber + "</title></head>");

        // Get the dispatcher; it gets the banner to the user
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/banner");

        if (dispatcher != null) {
            dispatcher.include(request, response);
        }

        if (itemNumber != null) {
            try {
            	Item item = itemDB.getItemByItemNumber( Integer.parseInt(itemNumber) );
            	
            	out.println("<center> <table>");
            	
            	out.println("<tr>" + "<td bgcolor=\"#ffffaa\">" + 
                    	"<a href=\"" +
                        response.encodeURL(request.getContextPath() + "/items?itemNumber=" + item.getItemNumber()) +
                        "\"> <strong>" + item.getName() + "&nbsp; </strong></a></td>" +
                        "<td bgcolor=\"#ffffaa\">" + item.getPrice() + "</td>" +
                        "<td bgcolor=\"#ffffaa\">" + 
                        "<a href=\"" + response.encodeURL(request.getContextPath() + "/menu?itemNumber=" + itemNumber) +
                        "\"> <strong>Add to cart" + "&nbsp; </strong></a></td></tr>");

            	out.println("<img src=\"" + request.getContextPath() + item.getImagePath()+ 
            	"\" alt=" + item.getName() +  "\"width=\"300\" height=\"300\" \">");
            	
            	out.println("</center></body></html>");
            } catch (Exception ex) {
                response.resetBuffer();
                throw new ServletException(ex);
            }
        }

        out.println("</body></html>");
        out.close();
    }

    public String getServletInfo() {
        return "The BookDetail servlet returns information about" +
        "any book that is available from the bookstore.";
    }
}
